package com.wwwzh.himalayafm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.live_adapter.BroadAdapter;
import com.wwwzh.himalayafm.callback.IsScrollButtonListener;
import com.wwwzh.himalayafm.entity.entity_broadtwo.BroadTotalEntity;
import com.wwwzh.himalayafm.retrofit.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BroadTwoActivity extends AppCompatActivity implements Callback<BroadTotalEntity>, View.OnClickListener, IsScrollButtonListener {
    private String text;
    private int pageNum = 1;
    private BroadAdapter adapter;
    private HttpService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_two);
        TextView tv = (TextView) findViewById(R.id.two_broad_tv);
        RecyclerView recycler = (RecyclerView) findViewById(R.id.two_broad_recycler);
        Button button = (Button) findViewById(R.id.finish_btn);
        button.setOnClickListener(this);
        Intent intent = getIntent();
        text = intent.getStringExtra("text");
        tv.setText(text);
        adapter = new BroadAdapter(this, this);
        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        switch (text) {
            case "本地台":
                service.getResult(pageNum, 20, "110000", 2).enqueue(this);
                break;
            case "国家台":
                service.getResult(pageNum, 20, null, 1).enqueue(this);
                break;
            case "网络台":
                service.getResult(pageNum, 20, null, 3).enqueue(this);
                break;

        }
        recycler.setAdapter(adapter);

    }


    @Override
    public void onResponse(Call<BroadTotalEntity> call, Response<BroadTotalEntity> response) {
        if (response.body().getResult() != null) {
            adapter.addAll(response.body().getResult());
        }

    }

    @Override
    public void onFailure(Call<BroadTotalEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void isScrollbutton() {

            pageNum++;
            switch (text) {
            case "本地台":
                service.getResult(pageNum, 20, "110000", 2).enqueue(this);
                break;
                case "国家台":
                    service.getResult(pageNum, 20, null, 1).enqueue(this);
                    break;
                case "网络台":
                    service.getResult(pageNum, 20, null, 3).enqueue(this);
                    break;
            }
    }
}
